package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import controller.DataBaseInteactor;

import model.LogIn;

/**
 * The frame for user to register.
 * 
 * @author Richard Lee
 * @version 52813
 */
public class RegisterView extends JFrame {

	/**
	 * Default serial version id.
	 */
	private static final long serialVersionUID = -2322424958528170143L;
	
	/**
	 * GUI components.
	 */
	private final JPanel buttonPanel = new JPanel(new FlowLayout());
	private final JPanel idPanel = new JPanel(new FlowLayout());
	private final JPanel passwordPanel = new JPanel(new FlowLayout());
	private final JPanel comfPanel = new JPanel(new FlowLayout());
	private final JPanel firstNamePanel = new JPanel(new FlowLayout());
	private final JPanel lastNamePanel = new JPanel(new FlowLayout());
	private final JPanel rolePanel = new JPanel(new FlowLayout());
	private final JPanel confPanel = new JPanel(new FlowLayout());
	private final JButton register = new JButton("Register");
	private final JButton cancle = new JButton("Cancel");
	private final JLabel idLabel = new JLabel("New User ID: ");
	private final JLabel pwLabel = new JLabel("New Password: ");
	private final JLabel comfPwLabel = new JLabel("Confirm Password: ");
	private final JLabel firstName = new JLabel("First Name: ");
	private final JLabel lastName = new JLabel("Last Name: ");
	private final JLabel roleLabel = new JLabel("Role: ");
	private final JLabel confLable = new JLabel("Conference: ");
	private final JTextField idField = new JTextField(10);
	private final JTextField pwField = new JTextField(10);
	private final JTextField comfPwField = new JTextField(10);
	private final JTextField firstNameField = new JTextField(10);
	private final JTextField lastNameField = new JTextField(10);
	
	/**
	 * List of roles.
	 */
	private String[] roleString = {"Author", "Reviewer", "SubProgram Chair", "Program Chair"};
	
	/**
	 * Combobox for roles.
	 */
	private JComboBox<String> roleList = new JComboBox<String>(roleString);
	
	/**
	 * Combbox for existing conferences.
	 */
	private JComboBox<String> conferenceList;
	
	/**
	 * Database interactor.
	 */
	private final DataBaseInteactor interactor;
	
	/**
	 * Constructor.
	 * 
	 * @param DBI Database interactor.
	 * @param theConferenceList the list of conferences.
	 */
	public RegisterView(DataBaseInteactor DBI, JComboBox<String> theConferenceList) {
		interactor = DBI;
		setLayout(new BorderLayout());
		theConferenceList.removeItemAt(0);
		conferenceList = theConferenceList;
		idPanel.add(idLabel);
		idPanel.add(idField);
		passwordPanel.add(pwLabel);
		passwordPanel.add(pwField);
		comfPanel.add(comfPwLabel);
		comfPanel.add(comfPwField);
		firstNamePanel.add(firstName);
		firstNamePanel.add(firstNameField);
		lastNamePanel.add(lastName);
		lastNamePanel.add(lastNameField);
		rolePanel.add(roleLabel);
		rolePanel.add(roleList);
		confPanel.add(confLable);
		confPanel.add(theConferenceList);
		buttonPanel.add(register);
		buttonPanel.add(cancle);
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		p.add(idPanel);
		p.add(passwordPanel);
		p.add(comfPanel);
		p.add(firstNamePanel);
		p.add(lastNamePanel);
		p.add(rolePanel);
		p.add(confPanel);
		p.add(buttonPanel);
		add(p);
	}
	
	/**
	 * Set this frame visiable.
	 */
	public void makeRegisterView() {
		setVisible(true);
		pack();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		registerAction();
	}
	
	/**
	 * Add actions to buttons.
	 */
	private void registerAction() {
		register.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String inputID = idField.getText();
				String inputPW = pwField.getText();
				String inputComfPw = comfPwField.getText();
				String inputFirstName = firstNameField.getText();
				String inputLastName = lastNameField.getText(); 
				String inputRole = (String) roleList.getSelectedItem();
				String inputConference = (String) conferenceList.getSelectedItem();
				LogIn login = new LogIn(inputID, inputPW, inputComfPw, inputFirstName, 
						inputLastName, inputRole, inputConference, interactor);
				//while(!login.newUser()) {
					if(login.newUser()) {
						JOptionPane.showMessageDialog(null, "User Registered");
					} else {
						JOptionPane.showMessageDialog(null, "Check the password again");
						comfPwField.setText("");
						pwField.setText("");
						pwField.requestFocus();
					}
				//}
				dispose();
				LogInFrame loginframe = new LogInFrame(interactor);
				loginframe.setVisible(true);
			}
		});
		
		cancle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
	}
}
